package com.gu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gu.entity.EduCourse;
import com.gu.entity.EduTeacher;
import com.gu.service.EduCourseService;
import com.gu.service.EduTeacherService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 05-31-11:24
 */
@RestController
@CrossOrigin
@Api(tags = "前台首页显示组件")
@RequestMapping("/eduService/index")
public class IndexController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduTeacherService eduTeacherService;

    @ApiOperation("前台首页显示")
    @GetMapping("getTeacherAndCourse")
    //加入到redis的缓存中
    @Cacheable(value = "teacherAndCourse",key = "'selectTC'")
    public CommonResult getTeacherAndCourse() {
        Page<EduCourse> coursePage = eduCourseService.getCourseForIndex();
        Page<EduTeacher> teacherPage = eduTeacherService.getTeacherForIndex();
        List<EduCourse> courseList = coursePage.getRecords();
        List<EduTeacher> teacherList = teacherPage.getRecords();
        return (courseList.size() > 0 && teacherList.size() > 0) ? CommonResult.success().data("courseList", courseList)
                .data("teacherList", teacherList) : CommonResult.error();

    }
}
